Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB.NET] Fermare un loop
Forum - C# / VB.NET - [VB.NET] Fermare un loop

Avatar
danduz97 (Normal User)
Pro


Messaggi: 130
Iscritto: 13/03/2011

Segnala al moderatore
Postato alle 16:13
Martedì, 16/07/2013
Salve a tutti.

sto sviluppando un software che permette di copiare sia directories che files. quindi ho creato il mio sub mettendo tutto sotto Try (nel caso ci fosse qualche errore il programma non si bloccherebbe)

solo che quando vado a testare il mio programma simulando un errore, si genera un loop infinito di eccezioni(sottoforma di MessageBox).. come farei a fermarlo facendo comparire una sola MsgBox?

ecco il mio sorgente:

Codice sorgente - presumibilmente VB.NET

  1. Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
  2.  
  3.  
  4.         Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
  5.  
  6.        
  7.         If Not System.IO.Directory.Exists(destinationPath) Then
  8.             System.IO.Directory.CreateDirectory(destinationPath)
  9.         End If
  10.  
  11.         Dim fileSystemInfo As System.IO.FileSystemInfo
  12.  
  13.         Try
  14.  
  15.             For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
  16.                 Dim destinationFileName As String =
  17.                     System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
  18.  
  19.            
  20.                 If TypeOf fileSystemInfo Is System.IO.FileInfo Then
  21.  
  22.                     System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
  23.  
  24.                 Else
  25.                
  26.                     CopyDirectory(fileSystemInfo.FullName, destinationFileName)
  27.                 End If
  28.  
  29.             Next
  30.  
  31.         Catch ex As Exception
  32.          
  33.             MsgBox("errore")
  34.          
  35.         End Try
  36.  
  37.  
  38.  
  39.  
  40.     End Sub




Grazie infinite! :k:

Ultima modifica effettuata da danduz97 il 16/07/2013 alle 16:16
PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 17:38
Martedì, 16/07/2013
Testo quotato

Postato originariamente da danduz97:

Salve a tutti.

sto sviluppando un software che permette di copiare sia directories che files. quindi ho creato il mio sub mettendo tutto sotto Try (nel caso ci fosse qualche errore il programma non si bloccherebbe)

solo che quando vado a testare il mio programma simulando un errore, si genera un loop infinito di eccezioni(sottoforma di MessageBox).. come farei a fermarlo facendo comparire una sola MsgBox?

ecco il mio sorgente:

Codice sorgente - presumibilmente VB.NET

  1. Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
  2.  
  3.  
  4.         Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
  5.  
  6.        
  7.         If Not System.IO.Directory.Exists(destinationPath) Then
  8.             System.IO.Directory.CreateDirectory(destinationPath)
  9.         End If
  10.  
  11.         Dim fileSystemInfo As System.IO.FileSystemInfo
  12.  
  13.         Try
  14.  
  15.             For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
  16.                 Dim destinationFileName As String =
  17.                     System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
  18.  
  19.            
  20.                 If TypeOf fileSystemInfo Is System.IO.FileInfo Then
  21.  
  22.                     System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
  23.  
  24.                 Else
  25.                
  26.                     CopyDirectory(fileSystemInfo.FullName, destinationFileName)
  27.                 End If
  28.  
  29.             Next
  30.  
  31.         Catch ex As Exception
  32.          
  33.             MsgBox("errore")
  34.          
  35.         End Try
  36.  
  37.  
  38.  
  39.  
  40.     End Sub




Grazie infinite! :k:



Ti consiglio di mettere il try nel ciclo For, e prima(o dopo :-?) del MsgBox mettere "Exit For"! :k:

PM Quote
Avatar
danduz97 (Normal User)
Pro


Messaggi: 130
Iscritto: 13/03/2011

Segnala al moderatore
Postato alle 18:31
Martedì, 16/07/2013
Testo quotato

Postato originariamente da LittleHacker:

Ti consiglio di mettere il try nel ciclo For, e prima(o dopo :-?) del MsgBox mettere "Exit For"! :k:



Grazie infinite! era esattamente quello che ci voleva! Grande! :cheer:

PS: si, andava messo prima l'exit for :k:

Ultima modifica effettuata da danduz97 il 16/07/2013 alle 18:32
PM Quote